// comhan.c
// Joseph Craig, Rebecca Rush, Steven Andryzcik
// Version 6 - 12/08/2011

#include <string.h>

#include "commands.h"
#include "common.h"
#include "mpx_supt.h"
#include "process.h"

/*********************************** Macros ***********************************/
#define MAX_ARGS 18

/****************************** Global Variables ******************************/
char prompt[17] = "\n> ";

extern char *command_names[NUM_FUNCTIONS];
extern int quitting;
extern void (*commands[NUM_FUNCTIONS])(int, char **);

/****************************** Command Handler *******************************/
// comhan
// Parameters: none
// Return: void
// Functions called: system, print, sys_req,
// strlen, strtok, strcmp, make_empty
// Comhan is the main system process that controls our entire
// operating system. It is loaded in as a process in main.c. 
void comhan() {
	const char *separator = " ";
	
	char
		buffer[BUFSIZ],
		i,
		*args[MAX_ARGS],
		*command,
		*token;
	int
		found,
		numargs,
		size;

	// Clearing the terminal doesn't work? O_o
	// sys_req(CLEAR, TERMINAL, NULL, NULL);
	system("cls");
	
	print(    
		"       __    __            _     _                  _____              \n"
		"      / / /\\ \\ \\___   ___ | | __(_) ___  ___ ___    \\_   \\_ __   ___   \n"
		"      \\ \\/  \\/ / _ \\ / _ \\| |/ /| |/ _ \\/ _ | __|    / /\\/ '_ \\ / __|  \n"   
		"       \\  /\\  / (_) | (_) |   < | |  __/  __|__ \\ /\\/ /_ | | | | (__ _ \n"   
		"        \\/  \\/ \\___/ \\___/|_|\\_\\|_|\\___|\\___|___/ \\____/ |_| |_|\\___(_)\n"   
		"                                     _____  __                         \n"  
		"                             /\\/\\   / _ \\ \\/ /                         \n" 
		"                            /    \\ / /_)/\\  /                          \n"
		"                           / /\\/\\ | ___/ /  \\                          \n"
		"                           \\/    \\|/    /_/\\_\\                         \n",  
		TRUE
	);
	
	while (!quitting) {
		found   = 0;
		numargs = 0;
		size    = BUFSIZ;
		
		/* Print the input prompt and request a command.
		 */
		print(prompt, TRUE);
		sys_req(READ, TERMINAL, buffer, &size);
		
		/* If the user did not enter a command, skip to the next prompt.
		 */
		if (strlen(buffer) == 1) {
			continue;
		}
		
		/* Determine the command and its arguments.
		 */
		buffer[strlen(buffer) - 1] = '\0';
		command = strtok(buffer, separator);
		
		while ((token = strtok(NULL, separator)) != NULL) {
			args[numargs++] = token;
		}
		
		/* If the user entered a valid command, run it.
		 */
		for (i = 0; !found && i < NUM_FUNCTIONS; i++) {
			int result = strcmp(command, command_names[i]);
			if (result == 0) {
				commands[i](numargs, args);
				found = TRUE;
			}
		}
		
		/* If the command was not found, print an error message.
		 */
		if (!found) {
			print("\n\tThe requested command does not exist.\n\n", TRUE);
		}
	}
    
	/* Clean up and end OS process execution.
	 */
	make_empty();
	sys_req(EXIT, NO_DEV, NULL, NULL);
}